package com.meilai.project.controller.mini;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.intentionScheme.BuildingPointVO;
import com.meilai.project.dto.business.saleOrder.SaleIntentionOrderDTO;
import com.meilai.project.dto.business.saleOrder.SaleIntentionOrderSaveDTO;
import com.meilai.project.dto.business.saleOrder.SaleMapSearchDTO;
import com.meilai.project.entity.business.customer.CustomerInfo;
import com.meilai.project.service.business.saleOrder.SaleIntentionOrderService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.business.saleOrder.SaleIntentionOrderVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ykb
 * @since 2022-03-05
 */
@Api(value = "销售意向订单controller", tags = {"销售意向订单"})
@RequestMapping("api/mini/sale_intention_order")
@RestController
@Validated
public class SaleIntentionOrderController {

	@Autowired
	private SaleIntentionOrderService service;

	@PostMapping("search")
	@ApiOperation("地图筛选")
	public ApiResult<List<BuildingPointVO>> search(@RequestBody SaleMapSearchDTO searchDTO) {
		List<BuildingPointVO> result = service.search(searchDTO);
		return ApiResult.ok(result);
	}

	@ApiOperation(value = "销售意向单列表", notes = "销售意向单列表", response = SaleIntentionOrderVO.class)
	@ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
			@ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
			@ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
			@ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
			@ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
	@ResponseBody
	@GetMapping("list")
	public CommonResult list(
			@ApiParam(name = "page", value = "当前页", required = true)
			@RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
			@ApiParam(name = "page_size", value = "每页数量", required = true)
			@RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,
			@ApiParam(name = "beginDate", value = "时间段搜索的起始，yyyy-MM-dd", required = false)
			@RequestParam(value = "beginDate", required = false) String beginDate,
			@ApiParam(name = "endDate", value = "时间段搜索的结束，yyyy-MM-dd", required = false)
			@RequestParam(value = "endDate", required = false) String endDate,
			@ApiParam(name = "building_area_id", value = "项目id", required = false)
			@RequestParam(value = "building_area_id", required = false) Long building_area_id,
			@ApiParam(name = "media_type_id", value = "媒体类型ID", required = false)
			@RequestParam(value = "media_type_id", required = false) Long media_type_id,
			@ApiParam(name = "sale_id", value = "销售员ID", required = false)
			@RequestParam(value = "sale_id", required = false) Long sale_id,
			@ApiParam(name = "customer_id", value = "客户ID", required = false)
			@RequestParam(value = "customer_id", required = false) Long customer_id
	) {

		SaleIntentionOrderDTO searchDTO = new SaleIntentionOrderDTO();
		searchDTO.setBuilding_area_id(building_area_id);
		searchDTO.setMedia_type_id(media_type_id);
		searchDTO.setSale_id(sale_id);
		searchDTO.setCustomer_id(customer_id);

		return new CommonResult().success(service.selectPageList(page, page_size,beginDate,endDate,searchDTO));
	}


	@PostMapping("save")
	@ApiOperation("保存")
	public ApiResult<SaleIntentionOrderSaveDTO> save(@ApiParam("json") @Validated @RequestBody SaleIntentionOrderSaveDTO req) {
		try {
			Long sale_id = ThreadLocalUserUtil.get().getId();
			if (sale_id == null) {
				return ApiResult.err("请先登录！");
			}
			req.setSale_id(sale_id);
			return service.save(req);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResult.err("保存失败，请联系管理员！");
		}
	}

	@GetMapping("listCustomerOfSale")
	@ApiOperation("获取当前销售员维护的客户")
	public ApiResult<List<CustomerInfo>> listCustomerOfSale() {
		try {
			Long sale_id = ThreadLocalUserUtil.get().getId();
			if (sale_id == null) {
				return ApiResult.err("请先登录！");
			}
			List<CustomerInfo> listCustomerInfo = service.listCustomerOfSale(sale_id);
			return ApiResult.ok(listCustomerInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiResult.err("保存失败，请联系管理员！");
		}
	}


}
